<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分区方案名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model.trim="model.name" placeholder="请输入分区方案名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="匹配类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="matchType">
              <j-search-select-tag type="list" v-model="model.matchType" dict="logistics_match_type" placeholder="请选择匹配类型" :disabled="title === '编辑'" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="providerId">
              <j-search-select-tag type="list" v-if="title === '新增'" v-model="model.providerId" dict="hz_logistics_provider,name,id,logistics,is_enable='Y'" placeholder="请选择服务商" @change="providerIdChange" />
              <j-search-select-tag type="list" v-else v-model="model.providerId" dict="hz_logistics_provider,name,id,logistics" placeholder="请选择服务商" @change="providerIdChange" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物流渠道" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelId">
              <j-search-select-tag ref="channelIdRef" type="list" v-model="model.channelId" placeholder="请选择物流渠道" :disabled="title === '编辑'" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="方案状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isEnable">
              <j-switch v-model="model.isEnable"  ></j-switch>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="备注说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注说明(最多150字)" :maxLength="150" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'LogisticsPartitionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        model:{
          isEnable: 'N'
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          name: [
            { required: true, message: '请输入物分区方案名称!'},
          ],
          matchType: [
            { required: true, message: '请选择匹配类型!'},
          ],
          channelId: [
            { required: true, message: '请选择物流渠道!'},
          ],
          providerId: [
            { required: true, message: '请选择服务商!'},
          ]
        },
        url: {
          add: "/logistics/part/logisticsPartition/add",
          edit: "/logistics/part/logisticsPartition/edit",
          queryById: "/logistics/part/logisticsPartition/queryById",
          list: "/logistics/channel/list"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(this.model.id){
          this.providerIdChange(this.model.providerId);
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      providerIdChange(){
        if(!this.model.id){
          this.$set(this.model, 'channelId', '');
        }
        let parmas = {
          column: 'createTime',
          order: 'desc',
          providerId: this.model.providerId,
          pageNo: 1,
          pageSize: 99999
        }
        if(this.title === '新增'){
          parmas.isEnable = 'Y';
        }
        getAction(this.url.list, parmas).then((res) => {
          if(res.success){
            let options = res.result && res.result.records && res.result.records.map(item => {
              return {
                value: item.id,
                text: item.channelName
              }
            })
            this.$refs.channelIdRef.setCurrentDictOptions(options);
          }else{
            this.$message.warning(res.message);
          }
        })
      }
    }
  }
</script>